package com.shigen.demo.algorithm;

import cn.hutool.core.util.StrUtil;
import java.util.Arrays;

/**
 * @author shigenfu
 * @date 2023/9/26 11:15 上午
 */
public class AutoUpgradeVersionUtil {

    public static void upgradeVersion(int[] nums, int index) {
        if (index == 0) {
            nums[index] += 1;
        } else {
            int value = nums[index] + 1;
            // 未产生进位
            if (value < 10) {
                nums[index] = value;
            } else {
                // 产生进位
                nums[index] = 0;
                upgradeVersion(nums, index - 1);
            }
        }
    }

    public static String autoUpgradeVersion(String version) {
        if (StrUtil.isEmpty(version)) {
            version = "0.0.1";
        }
        String[] array = version.split("\\.");
        int[] nums = Arrays.stream(array).mapToInt(Integer::parseInt).toArray();
        upgradeVersion(nums, nums.length - 1);
        return StrUtil.join(".", nums);
    }

    public static void main(String[] args) {
        String s = autoUpgradeVersion("1.9.9");
        System.out.println(s);
    }

}
